package servlet;

import biz.ForumsBiz;
import biz.HomePostsBiz;
import biz.impl.ForumsBizImpl;
import biz.impl.HomePostBizImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.example.bean.HomePostsInfo;
import org.example.bean.MemberInfo;
import org.example.bean.ReplyPostsInfo;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/homeposts")
public class HomePostsServlet extends HttpServlet {
    private HomePostsBiz hpb;
    private ForumsBiz fb;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String type = req.getParameter("type");
        if ("id".equals(type)){
            id(req,resp);
        }else if("save".equals(type)){
            save(req,resp);
        }else if("find".equals(type)){
            find(req,resp);
        }else if("save2".equals(type)){
            save2(req,resp);
        }
    }
    protected void id(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SQL_SESSION_FACTORY");
        SqlSession sqlSession = ssf.openSession();
        int id = Integer.parseInt(request.getParameter("id"));
        hpb = new HomePostBizImpl(sqlSession);
        fb = new ForumsBizImpl(sqlSession);
        session.setAttribute("HOME_POST_BY_ID",hpb.findHomePostsByForumsId(id));
        session.setAttribute("FORUMS_INFO",fb.findForumsById(id));
        sqlSession.close();
        response.sendRedirect("main.jsp");
    }
    protected void save(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SQL_SESSION_FACTORY");
        SqlSession sqlSession = ssf.openSession();

        int forumId = Integer.parseInt(request.getParameter("forumId"));
        int userId = Integer.parseInt(request.getParameter("userId"));
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String mood = request.getParameter("mood");
        HomePostsInfo hpi = new HomePostsInfo();
        hpi.setForumId(forumId);
        hpi.setUserId(userId);
        hpi.setHomePostTitle(title);
        hpi.setHomePostContexts(content);
        hpi.setSayMood(mood);

        hpb = new HomePostBizImpl(sqlSession);
        hpb.saveHomePosts(hpi);
        MemberInfo mi = hpb.findMemberByUserId(userId);
        int i = mi.getIntegral()+3;

        if (i<30){

        }else if (i<50){
            hpb.saveUserById(2,userId);
        }else if (i<80){
            hpb.saveUserById(3,userId);
        }else if (i<100){
            hpb.saveUserById(4,userId);
        }else if (i<130){
            hpb.saveUserById(5,userId);
        }else if (i<150){
            hpb.saveUserById(6,userId);
        }else if (i<200){
            hpb.saveUserById(7,userId);
        }else {
            hpb.saveUserById(8,userId);
        }
        mi.setIntegral(i);
        hpb.saveMemberByUserId(mi);

        sqlSession.close();
        //response.sendRedirect("main.jsp");
        response.sendRedirect("homeposts?type=id&id="+forumId);
    }
    protected void find(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SQL_SESSION_FACTORY");
        SqlSession sqlSession = ssf.openSession();

        hpb = new HomePostBizImpl(sqlSession);
        session.setAttribute("HOME_POST_INFO",hpb.findHomePostById(id));
        session.setAttribute("REPLAY_LIST",hpb.findReplyPostsByHomePostId(id));
        sqlSession.close();
        response.sendRedirect("replay.jsp");
    }
    protected void save2(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        ServletContext application = session.getServletContext();
        SqlSessionFactory ssf = (SqlSessionFactory) application.getAttribute("SQL_SESSION_FACTORY");
        SqlSession sqlSession = ssf.openSession();
        int homeId = Integer.parseInt(request.getParameter("homeId"));
        int userId = Integer.parseInt(request.getParameter("userId"));
        String content = request.getParameter("content");
        ReplyPostsInfo rpi = new ReplyPostsInfo();
        rpi.setHomePostId(homeId);
        rpi.setUserId(userId);
        rpi.setReplyPostContexts(content);

        hpb = new HomePostBizImpl(sqlSession);
        hpb.saveReply(rpi);
        MemberInfo mi = hpb.findMemberByUserId(userId);
        int i = mi.getIntegral()+1;

        if (i<30){

        }else if (i<50){
            hpb.saveUserById(2,userId);
        }else if (i<80){
            hpb.saveUserById(3,userId);
        }else if (i<100){
            hpb.saveUserById(4,userId);
        }else if (i<130){
            hpb.saveUserById(5,userId);
        }else if (i<150){
            hpb.saveUserById(6,userId);
        }else if (i<200){
            hpb.saveUserById(7,userId);
        }else {
            hpb.saveUserById(8,userId);
        }
        mi.setIntegral(i);
        hpb.saveMemberByUserId(mi);

        sqlSession.close();
        response.sendRedirect("homeposts?type=find&id="+homeId);
    }
}
